package org.test.test;

import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.test.entrasalida.excel.AbstractExcel;
import org.test.entrasalida.excel.IExcelWriter;
import org.test.entrasalida.excel.ReportPOIUtils;



public class SimpleReportExcelWriter extends AbstractExcel implements IExcelWriter {

	/** REPORT_1_SIZES */
	private static final int[] REPORT_SIZES = new int[] {2926,2926,2560,2560,2560,3291,3291,6217,6217,2560,2560,3657,2560,2560,2560,2560,2560,2560,3657,2926,2560,6217,6217,2560,2560,2560,2560,2560,2560,2560,2560,2560,2560,2560,2560,6217};
	
	/**
	 * setAdditionalStyles
	 * 
	 */
	public void setAdditionalStyles() {
		return;
	}

	public byte[] getReport() throws Exception {
		createSheet("sheet");
		writeReport();
		return out.toByteArray();
	}
	
	/**
	 * Write the excel report
	 * 
	 * @param listCrossType
	 *            listCrossType
	 * @param listDeliverable
	 *            listDeliverable
	 * @throws Exception 
	 */
	public void writeReport() throws Exception {

		long initTime = System.currentTimeMillis();
		writeContent();
		writeTableHeader();
		writeHeader();
		applySheetStyle(0, REPORT_SIZES);
		saveExcel();
		long finalTime = System.currentTimeMillis();
		System.out.println("ExcelGenerationTime: " + (finalTime - initTime));
	}	
	
	public void writeHeader() {
		HSSFSheet sheet = sheets.get(0);
		HSSFRow row2 = sheet.createRow(2);
		// Line 2
		addCell(0, row2, "title",
				ReportPOIUtils.getTitle12ReportFormat());
		sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 3));
		addCell(4, row2, new Date(),ReportPOIUtils
				.getTitle10ReportFormat());
		sheet.addMergedRegion(new CellRangeAddress(2, 2, 4, 7));
		row2.setHeightInPoints(30f);

		// line 4
		HSSFRow row4 = sheet.createRow(4);
		addCell(0, row4, "title 2" , ReportPOIUtils
				.getSubTitle10BoldFormat());
		sheet.addMergedRegion(new CellRangeAddress(4, 4, 0, 1));
		addCell(2, row4, "code",
				ReportPOIUtils.getSubTitle10BoldFormat());
	}

	public void writeTableHeader() {
		int i = 0;
		HSSFRow row6 = sheets.get(0).createRow(6);
		addCell(i++, row6, "Date", ReportPOIUtils
				.getHeaderTableFormat());
		addCell(i++, row6, "Text", ReportPOIUtils
				.getHeaderTableFormat());
		addCell(i++, row6, "Number", ReportPOIUtils
				.getHeaderTableFormat());
		
		updateNbColum(0, row6);
	}

	public void writeContent() {
		int col=0;
		HSSFRow row = sheets.get(0).createRow(7);//on the line number 7
		
		addCell(col++, row, new Date(), ReportPOIUtils
				.getTableContentDateFormat("d/m/yy"));
		addCell(col++, row, "simple text", ReportPOIUtils
				.getTableContentFormat());
		addCell(col++, row, 1, ReportPOIUtils
				.getTableContentFormat());
	}
	
	
}
